﻿using System;
using System.Drawing;
using Finance.Data.Entities;
using Finance.Properties;
using TodoSoft;
using TodoSoft.Reporting;

namespace Finance.Reports
{
	public class DebtsReport : Report
	{
		public DebtsReport(Action<Guid, DocumentType> openDocumentAction)
		{
			var documentTypeReportGrouping = new ReportGrouping("documentTypeGroup");
			documentTypeReportGrouping.Fields.Add(ReportDataField.First("documentTypeGroup", 110, Resources.DebtsReport_ContractorColumnName));
			var docTypeAmountField = ReportDataField.Sum("amount", 100, Resources.DebtsReport_ContractorAmountColumnName);
			docTypeAmountField.StringFormat = new StringFormat { Alignment = StringAlignment.Far };
			docTypeAmountField.FormatArgument = "F";
			documentTypeReportGrouping.Fields.Add(docTypeAmountField);
			documentTypeReportGrouping.HeaderRowVisible = true;
			documentTypeReportGrouping.Font = new Font("Arial", 10, FontStyle.Bold);
			Groupings.Add(documentTypeReportGrouping);

			var contractorGrouping = new ReportGrouping("contractorid") {Expanded = false};
			contractorGrouping.Fields.Add(ReportDataField.First("contractorname", 110, ""));
			var contractorAmountField = ReportDataField.Sum("amount", 100, "");
			contractorAmountField.StringFormat = new StringFormat { Alignment = StringAlignment.Far };
			contractorAmountField.FormatArgument = "F";
			contractorGrouping.Fields.Add(contractorAmountField);
			contractorGrouping.HeaderRowVisible = false;
			Groupings.Add(contractorGrouping);

			var documentGrouping = new ReportGrouping("documentid");
			var datetimeField = ReportDataField.First("docdatetime", 80, "");
			datetimeField.FormatArgument = "d";
			datetimeField.Padding = new Margins(15,0,0,0);
			if (openDocumentAction != null)
				datetimeField.CellClick += data => openDocumentAction((Guid)data["documentid"], (DocumentType)data["typecode"]);

			documentGrouping.Fields.Add(datetimeField);
			var documentAmountField = ReportDataField.First("amount", 70, "");
			documentAmountField.StringFormat = new StringFormat { Alignment = StringAlignment.Far };
			documentAmountField.FormatArgument = "F";
			documentGrouping.Fields.Add(documentAmountField);
			var documentRunningAmountField = ReportDataField.RunningValue("amount", 70, "");
			documentRunningAmountField.StringFormat = new StringFormat { Alignment = StringAlignment.Far };
			documentRunningAmountField.FormatArgument = "F";
			documentGrouping.Fields.Add(documentRunningAmountField);
			documentGrouping.HeaderRowVisible = false;
			Groupings.Add(documentGrouping);

			DataQuery =
				"select " +
				"document.typecode as typecode," +
				"case document.typecode " +
				"when " + (int)DocumentType.DohodZaimBack + " then '" + Resources.DebtsReport_IloanedColumnName + "' " +
				"when " + (int)DocumentType.RashodZaim + " then '" + Resources.DebtsReport_IloanedColumnName + "' " +
				"else '" + Resources.DebtsReport_IBorrowedType + "' " +
				"end as documentTypeGroup, " +
				"case document.typecode " +
				"when " + (int)DocumentType.DohodZaimBack + " then document.amount " +
				"when " + (int)DocumentType.DohodZaim + " then document.amount " + 
				"else -document.amount end as amount, " +
				"contractor.name as contractorname, " +
				"document.docdatetime as docdatetime," +
				"document.documentid as documentid, " +
				"document.contractorid as contractorid " +
				"from (select id as documentid, docdatetime, typecode, amount, contractorid from debtdocument " +
				"union all " +
				"select id as documentid, docdatetime, typecode, debtamount as amount, contractorid from repaydebtdocument) as document " +
				"inner join contractor on contractor.contractorid = document.contractorid " +
				"where docdatetime >= @Period_start and docdatetime <= @Period_end and " +
				"document.typecode <> " + (int)DocumentType.Dohod + " and " +
				"document.typecode <> " + (int)DocumentType.Rashod + " " +
				"order by contractorname, docdatetime";

			Parameters.Add(new PeriodReportParameter("Period")
				{
					MinValueQuery = "select min(d.docdatetime) from (select docdatetime from debtdocument union select docdatetime from repaydebtdocument) as d",
					MaxValueQuery = "select max(d.docdatetime) from (select docdatetime from debtdocument union select docdatetime from repaydebtdocument) as d",
				});
		}
	}
}
